const Sequelize = require('sequelize')
const db = require('./db')
// 将allowNull设置为false会将NOT NULL添加到列中，
const GithubUser = db.define(
  'github_user',
  {
    id: { type: Sequelize.STRING(100), primaryKey: true, allowNull: false },
    login: { type: Sequelize.STRING, allowNull: false },
    avatar_url: { type: Sequelize.STRING, allowNull: false },
    gravatar_id: { type: Sequelize.STRING, allowNull: true, defaultValue: '' },
    url: { type: Sequelize.STRING, allowNull: false },
    html_url: { type: Sequelize.STRING, allowNull: false },
    followers_url: { type: Sequelize.STRING, allowNull: false },
    following_url: { type: Sequelize.STRING, allowNull: false },
    gists_url: { type: Sequelize.STRING, allowNull: false },
    starred_url: { type: Sequelize.STRING, allowNull: false },
    subscriptions_url: { type: Sequelize.STRING, allowNull: false },
    organizations_url: { type: Sequelize.STRING, allowNull: false },
    repos_url: { type: Sequelize.STRING, allowNull: false },
    events_url: { type: Sequelize.STRING, allowNull: false },
    received_events_url: { type: Sequelize.STRING, allowNull: false },
    type: { type: Sequelize.STRING, allowNull: false },
    site_admin: {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    name: { type: Sequelize.STRING, allowNull: false, defaultValue: '' },
    company: { type: Sequelize.STRING, allowNull: false, defaultValue: '' },
    blog: { type: Sequelize.STRING, allowNull: true, defaultValue: '' },
    location: { type: Sequelize.STRING, allowNull: false, defaultValue: '' },
    email: { type: Sequelize.STRING, allowNull: false, defaultValue: '' },
    hireable: { type: Sequelize.STRING, allowNull: false, defaultValue: '' },
    bio: { type: Sequelize.STRING, allowNull: false, defaultValue: '' },
    public_repos: {
      type: Sequelize.INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    public_gists: {
      type: Sequelize.INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    followers: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0 },
    following: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0 },
    last_visit_time: {
      type: Sequelize.DATE,
      defaultValue: Sequelize.NOW,
      allowNull: false
    },
    created_at: {
      type: Sequelize.DATE,
      defaultValue: Sequelize.NOW,
      allowNull: false
    },
    updated_at: {
      type: Sequelize.DATE,
      defaultValue: Sequelize.NOW,
      allowNull: false
    }
  },
  {
    // 如果为 true 则表的名称和 model 相同，即 user
    // 为 false MySQL创建的表名称会是复数 users
    // 如果指定的表名称本就是复数形式则不变
    // freezeTableName: true,
    timestamps: false
  }
)
GithubUser.sync({ force: false })
  .then(res => {
    console.log('create table successfully!')
  })
  .catch(err => {
    console.log('create table error!', err)
  })
module.exports = GithubUser
